package dao

import (
	"xiaoercaidan/publicResources/models"
	"github.com/astaxie/beego/orm"
)

func InsertTable(table *models.Table) (error) {
	o := orm.NewOrm()
	_id, err := o.Insert(table)
	table.Id = _id
	return err
}

func QeryTableBlur(params map[string]string) (*[]models.Table) {

	o := orm.NewOrm()

	var tables []models.Table

	var qs orm.QuerySeter

	qs = o.QueryTable("table")

	for k, v := range params {
		qs = qs.Filter(k + "__exact", v)
	}

	qs.RelatedSel().All(&tables)

	if (len(tables) > 0) {
		return &tables
	} else {
		return nil
	}
}

func DeleteTables(shopId, tableIds  string) error {
	o := orm.NewOrm()
	_, err := o.Raw("delete from `table` where table_id in(" + tableIds + ") and shop_id = " + shopId).Exec()

	return err
}

func UpdateTable(table *models.Table) (error) {
	o := orm.NewOrm()

	_, err := o.QueryTable("table").Filter("id", table.Id).Update(orm.Params{
		"table_name": table.TableName,
		"shop_id": table.ShopId,
		"status": table.Status,
		"table_id": table.TableId,
		"table_size": table.TableSize,
		"q_rcode": table.QRcode,
	})

	return err
}